package com.example.AdminComponent;

import com.example.Utils.HttpUtil;
import com.example.Utils.ResultUtil;
import com.example.pojo.ParseTeacher;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class Teacher extends Box {
    JTable Table;
    Vector<String> teacher=new Vector<>();
    Vector<Vector<String>> teachers=new Vector<Vector<String>>();
    TableModel tm;
    public Teacher() {
        super(BoxLayout.Y_AXIS);
        JPanel jPanel=new JPanel();
        jPanel.setBackground(new Color(208, 210, 213, 179));
        jPanel.setMaximumSize(new Dimension(700,40));
        //jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        JButton b1=new JButton("修改");
        JButton b2=new JButton("删除");
        JButton b3=new JButton("查询");
        JButton b4=new JButton("添加");
        JLabel name=new JLabel("姓名");
        JLabel department=new JLabel("院系");
        JTextField t1=new JTextField(6);
        JTextField t2=new JTextField(6);
        Box box1= Box.createHorizontalBox();
        jPanel.add(name);
        jPanel.add(t1);
        jPanel.add(department);
        jPanel.add(t2);
        jPanel.add(b3);
        jPanel.add(Box.createHorizontalStrut(50));
        jPanel.add(b1);
        jPanel.add(b2);
        jPanel.add(b4);
        this.add(jPanel);
        //组装表格
        String []ss={"员工号","姓名","院系","课程"};
        for (int i = 0; i < ss.length; i++) {
            teacher.add(ss[i]);
        }
        requestPostData("http://localhost:8080/teacher");
        tm=new DefaultTableModel(teachers,teacher);
        Table=new JTable(tm){
            @Override
            public boolean isCellEditable(int row,int column){
                return false;
            }
        };
        //只能选中一行
        Table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();//单元格渲染器
        tcr.setHorizontalAlignment(JLabel.CENTER);//居中显示
        Table.setDefaultRenderer(Object.class, tcr);//设置渲染器
        JScrollPane scrollPane=new JScrollPane(Table);
        Table.setFont(new Font("宋体",Font.PLAIN,15));
        this.add(scrollPane);
        //查询按钮绑定
        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (t2.getText().trim().equals("")&&!t1.getText().trim().equals("")) {
                    requestGetData("http://localhost:8080/selectTeacher" + "?name=" + t1.getText());
                }else
                if (t1.getText().trim().equals("")&&!t2.getText().trim().equals("")) {
                    requestGetData("http://localhost:8080/selectTeacher" + "?department=" + t2.getText());
                } else  if (!t1.getText().trim().equals("")&&!t2.getText().trim().equals("")){
                    requestGetData("http://localhost:8080/selectTeacher" + "?name=" + t1.getText() + "&department=" + t2.getText());
                }else {
                    requestPostData("http://localhost:8080/teacher");
                }
                //刷新表格
                Table.updateUI();
            }
        });

        /*
         * 修改按钮
         * */
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = Table.getSelectedRow();//获取行
                //   DefaultTableModel dm= (DefaultTableModel) Table.getModel();//获取内容模型
                //弹出面板
                JFrame updateJFrame = new UpdateJFrameTeacher((String) tm.getValueAt(row, 0), (String) tm.getValueAt(row, 1),
                        (String) tm.getValueAt(row, 2), (String) tm.getValueAt(row, 3),Table);


            }
        });



        /*
         * 删除按钮
         *
         * */
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = Table.getSelectedRow();//获取行
                DefaultTableModel dm= (DefaultTableModel) tm;
                String name=(String) tm.getValueAt(row, 1);
                dm.removeRow(row);
                HttpUtil.sendGet("http://localhost:8080/teacher/delete/"+name);
                JOptionPane.showMessageDialog(null,"删除成功" );
            }
        });


        /*
         * 添加老师
         * */

        b4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                      new addTeacherJFrame();
            }
        });
    }





    //请求数据
    public  void requestPostData( String url){
        String result = HttpUtil.sendPost(url);
        Vector<ParseTeacher> parseTeachers = ResultUtil.ResultVectorTeacher(result);
        Vector<Vector<String>> ss = new Vector<>();
        for (int i = 0; i <parseTeachers.size(); i++) {
            Vector<String> s = new Vector<>();
            s.add(parseTeachers.get(i).getId());
            s.add(parseTeachers.get(i).getName());
            s.add(parseTeachers.get(i).getDepartment());
            s.add(parseTeachers.get(i).getCourse());
            ss.add(s);
        }
        teachers.clear();
        for (Vector<String> sv  : ss) {
            teachers.add(sv);
        }

    }



    public  void requestGetData( String url) {
        String result = HttpUtil.sendGet(url);
        Vector<ParseTeacher> parseTeachers = ResultUtil.ResultVectorTeacher(result);
        Vector<Vector<String>> ss = new Vector<>();
        for (int i = 0; i <parseTeachers.size(); i++) {
            Vector<String> s = new Vector<>();
            s.add(parseTeachers.get(i).getId());
            s.add(parseTeachers.get(i).getName());
            s.add(parseTeachers.get(i).getDepartment());
            s.add(parseTeachers.get(i).getCourse());
            ss.add(s);
        }
        teachers.clear();
        for (Vector<String> sv  : ss) {
            teachers.add(sv);
        }

    }

}
